1 00:00:00,790 --> 00:00:01,260 Okay. 2 00:00:01,270 --> 00:00:04,890 Today's lecture is going to dive deeper into the data type string. 3 00:00:04,900 --> 00:00:09,730 We're going to learn how to add strings together, how to get specific parts of a string, how to use 4 00:00:09,730 --> 00:00:12,190 escape characters and how to use the string library. 5 00:00:12,190 --> 00:00:13,180 So let's get started. 6 00:00:13,210 --> 00:00:17,260 Now we already know that a string is simply a collection of text, right? 7 00:00:17,260 --> 00:00:23,170 We can create a string using quotations such as single quotations or double quotations. 8 00:00:23,170 --> 00:00:27,340 And we can also create multi-line strings by using two brackets like this. 9 00:00:27,520 --> 00:00:32,830 However, we can also manipulate strings in many different ways, and we can do so by using the string 10 00:00:32,830 --> 00:00:33,610 library. 11 00:00:33,640 --> 00:00:39,610 A library is simply a collection of pre-written code that developers can use to complete tasks without 12 00:00:39,610 --> 00:00:41,710 having to implement the code yourself. 13 00:00:41,710 --> 00:00:47,950 Specifically, the string library is built into Lua and lets us do several things like make a string 14 00:00:47,950 --> 00:00:54,430 completely uppercase, lowercase, or split the string into multiple different sections based on a specific 15 00:00:54,430 --> 00:00:55,810 character occurrence. 16 00:00:55,810 --> 00:00:57,910 So let's go ahead and show this library in action. 17 00:00:57,910 --> 00:00:59,770 I'm going to create a variable here. 18 00:00:59,770 --> 00:01:02,950 And I'm just going to call this variable example string. 19 00:01:03,160 --> 00:01:06,910 And we can create a string of like hello world. 20 00:01:07,530 --> 00:01:12,810 And what I'm going to do is first call our print function and pass our example string. 21 00:01:12,810 --> 00:01:17,190 And that means we should expect to see Hello World get printed in the output. 22 00:01:17,190 --> 00:01:20,100 And then next what I'm going to do is I'm going to set example string. 23 00:01:20,100 --> 00:01:23,670 I'm going to override the value inside of there equal to. 24 00:01:23,670 --> 00:01:28,140 And then to access the string library we're going to type out string. 25 00:01:28,290 --> 00:01:32,340 And then we can use the dot operator to index the string library. 26 00:01:32,340 --> 00:01:37,170 And here we get to see all of the different functions available to us inside of this library. 27 00:01:37,200 --> 00:01:40,020 For example we can set a string to be all lowercase. 28 00:01:40,020 --> 00:01:42,210 We can set one to be all uppercase. 29 00:01:42,210 --> 00:01:43,800 We can reverse a string. 30 00:01:43,800 --> 00:01:45,120 We can get sub strings. 31 00:01:45,120 --> 00:01:48,720 And there's a whole bunch of other different functions inside of the string library. 32 00:01:48,720 --> 00:01:52,440 So what I'm going to do first is demonstrate the string dot lower function. 33 00:01:52,440 --> 00:01:54,060 So we'll autofill that here. 34 00:01:54,060 --> 00:01:56,670 And then we can just pass example string again. 35 00:01:56,670 --> 00:01:59,700 So it's going to grab the value of hello World. 36 00:01:59,700 --> 00:02:02,940 And it's going to make all of the characters in there lowercase. 37 00:02:02,940 --> 00:02:07,620 And then we can call our print function again and pass our example string. 38 00:02:07,950 --> 00:02:12,450 And we should expect Hello World to print here, but it should be all lowercase. 39 00:02:12,450 --> 00:02:16,710 And then once more I'm going to override the value inside of example string. 40 00:02:16,710 --> 00:02:18,510 And I'm going to set it equal to. 41 00:02:18,510 --> 00:02:21,420 And this time we're going to use the upper function. 42 00:02:21,420 --> 00:02:23,460 And then we can pass example string. 43 00:02:23,580 --> 00:02:26,160 And then we'll print out that string as well. 44 00:02:26,160 --> 00:02:28,740 So we should get all uppercase letters here. 45 00:02:29,500 --> 00:02:32,920 And then I'm also going to demonstrate the reverse function. 46 00:02:32,920 --> 00:02:37,660 So again we'll override example string and use the string dot reverse. 47 00:02:37,750 --> 00:02:40,300 And then we can pass example string once more. 48 00:02:40,630 --> 00:02:45,820 And then the last function I would like to demonstrate is the sub or sub string function. 49 00:02:46,330 --> 00:02:49,900 So we'll do example string equal to string dot sub. 50 00:02:49,900 --> 00:02:55,780 And what this does you can see the definition right here returns the sub string of s which is what we 51 00:02:55,780 --> 00:03:02,100 pass up here that starts at I which is an index and continues until J which is the second index. 52 00:03:02,110 --> 00:03:08,260 So what this means is we pass a string to it, and we basically grab a chunk out of that string and 53 00:03:08,260 --> 00:03:09,580 store it inside of a variable. 54 00:03:09,580 --> 00:03:11,350 Or that's what this function returns. 55 00:03:11,350 --> 00:03:18,370 So for example, if I wanted to extract the word world only in here I could use the string sub function 56 00:03:18,370 --> 00:03:23,110 pass where exactly the word world starts and ends. 57 00:03:23,110 --> 00:03:30,100 So for example here at the first index which would be one is h, two is E and so on. 58 00:03:30,100 --> 00:03:33,100 And then we get to W which would have its own index. 59 00:03:33,100 --> 00:03:36,640 And then the last letter here D would also have its own index. 60 00:03:36,640 --> 00:03:39,310 So what I'm going to do is I'm going to do string dot sub. 61 00:03:39,340 --> 00:03:41,410 I'm going to pass my example string. 62 00:03:41,680 --> 00:03:45,040 And I want to get a sub string of only world here. 63 00:03:45,040 --> 00:03:46,120 So we can count. 64 00:03:46,150 --> 00:03:52,870 This is one character 2345678. 65 00:03:52,870 --> 00:03:55,510 So we know that the W starts at eight. 66 00:03:55,510 --> 00:03:57,550 So we're going to pass eight here. 67 00:03:57,550 --> 00:04:01,990 And then 910 1112 it ends at 12. 68 00:04:01,990 --> 00:04:04,720 So we can pass 12 as the second argument. 69 00:04:04,720 --> 00:04:10,540 And what this function is going to do is it's going to extract the string of world and store it inside 70 00:04:10,540 --> 00:04:11,770 of our function right here. 71 00:04:12,320 --> 00:04:18,500 And before we do that, I'm also going to print our previous example when we reverse our string here. 72 00:04:18,590 --> 00:04:22,370 So now when we print we should get our default string here. 73 00:04:22,370 --> 00:04:24,050 We should get it Lowercased. 74 00:04:24,080 --> 00:04:25,940 Here we'll get it uppercase. 75 00:04:25,970 --> 00:04:27,770 This will print it reversed. 76 00:04:27,770 --> 00:04:33,260 And then last but not least this will store it as the word of world only. 77 00:04:33,260 --> 00:04:36,980 And then we can also print that out into the console to see that as well. 78 00:04:37,370 --> 00:04:41,810 And then one last thing I would like to show you is how to get the length of a string. 79 00:04:41,810 --> 00:04:43,640 And it's actually very easy to do. 80 00:04:43,670 --> 00:04:48,980 What we do is we first put a hashtag and then we can type out our string. 81 00:04:48,980 --> 00:04:54,620 And what this returns is a number that represents the exact length of the string. 82 00:04:54,620 --> 00:04:57,890 And we can go ahead and print this out in the console as well. 83 00:04:59,790 --> 00:05:02,330 So now let's go ahead and head to the test tab. 84 00:05:02,340 --> 00:05:04,620 Hit the drop down menu and press run. 85 00:05:06,860 --> 00:05:10,610 And as you can see, we get all of our different strings printed inside of the console. 86 00:05:10,640 --> 00:05:13,040 Here we get our regular string printed out. 87 00:05:13,040 --> 00:05:13,780 Hello world! 88 00:05:13,790 --> 00:05:16,400 Here we get it all completely lowercased. 89 00:05:16,430 --> 00:05:18,860 The next one is all uppercase. 90 00:05:18,890 --> 00:05:23,000 Here we have it reversed and then here. 91 00:05:23,090 --> 00:05:24,380 Oh that's right, we reversed it. 92 00:05:24,380 --> 00:05:26,000 So we actually didn't get world here. 93 00:05:26,000 --> 00:05:29,720 But instead we got a comma o l l e. 94 00:05:30,170 --> 00:05:32,240 And that's because we had the string reversed. 95 00:05:32,240 --> 00:05:35,930 So it's grabbing these last indexes instead of world. 96 00:05:35,960 --> 00:05:40,910 So if we actually wanted to grab world again we would have to reverse our string once more. 97 00:05:41,150 --> 00:05:47,000 But as you can see right here, it printed out the number five, which represents the length or the 98 00:05:47,000 --> 00:05:48,740 amount of characters inside of the string. 99 00:05:48,740 --> 00:05:54,410 As you can see, we have one, two, three, four, and five, so that number perfectly represents the 100 00:05:54,410 --> 00:05:55,430 length of the string. 101 00:05:56,150 --> 00:06:01,820 So what I'm going to do here is before we grab the substring of our example string, I'm going to reverse 102 00:06:01,820 --> 00:06:02,650 it once more. 103 00:06:02,660 --> 00:06:07,190 So example string is equal to string dot reverse and pass example string. 104 00:06:07,190 --> 00:06:11,720 And this should now store the word of world. 105 00:06:11,720 --> 00:06:14,180 So we can go ahead and hit run again. 106 00:06:14,900 --> 00:06:15,890 And there we go. 107 00:06:15,890 --> 00:06:17,930 We get our word world. 108 00:06:18,260 --> 00:06:21,770 It grabbed it from the eighth to the 11th index. 109 00:06:21,770 --> 00:06:28,400 And that function is inclusive, meaning it includes the letter that was at the eighth index, which 110 00:06:28,400 --> 00:06:35,780 was W, and it includes the letter that was at the last index, which is the letter D, and we still 111 00:06:35,780 --> 00:06:37,840 get the length of five characters. 112 00:06:37,850 --> 00:06:38,660 Perfect. 113 00:06:39,550 --> 00:06:44,570 Next, I'm going to show you how you can concatenate two strings together. 114 00:06:44,590 --> 00:06:45,740 What is concatenate? 115 00:06:45,760 --> 00:06:50,590 Well, concatenate is basically just a fancy word for adding two things together. 116 00:06:50,620 --> 00:06:54,620 If I have multiple strings, I would like to combine into a single string. 117 00:06:54,640 --> 00:06:56,990 This is where we would use concatenation. 118 00:06:57,010 --> 00:07:00,250 So what I'm going to do is I'm going to create a second string. 119 00:07:00,250 --> 00:07:03,760 I'm going to just call this variable example string two. 120 00:07:03,760 --> 00:07:05,590 And I'm going to store in there. 121 00:07:05,920 --> 00:07:08,200 It's a great day. 122 00:07:09,040 --> 00:07:11,180 And then I'm going to create a third variable. 123 00:07:11,200 --> 00:07:14,500 I'm going to call this example string three. 124 00:07:14,650 --> 00:07:21,160 And what I'm going to do is I'm going to reference my first example string, which by the time it reaches 125 00:07:21,160 --> 00:07:23,380 this point it will just be the word world. 126 00:07:23,380 --> 00:07:28,480 Because remember Lua executes code serially starting at the top and going down. 127 00:07:28,480 --> 00:07:31,780 And that's perfectly demonstrated by each of our print statements, right? 128 00:07:31,960 --> 00:07:37,270 We print out the string, we apply a change to the string, it prints that change out, and it keeps 129 00:07:37,270 --> 00:07:40,930 going down line by line, as demonstrated here in the console. 130 00:07:41,110 --> 00:07:44,770 So we'll get the word of world here. 131 00:07:44,770 --> 00:07:50,470 And then to concatenate it with this second string right here we are going to use two dots. 132 00:07:50,470 --> 00:07:52,660 So we do one and then two. 133 00:07:52,660 --> 00:07:58,030 And then we get to pass a second string which is going to be example string two. 134 00:07:58,060 --> 00:08:03,310 Now what this has done is it's joined these two strings together into a single string and stored it 135 00:08:03,310 --> 00:08:04,660 inside of this variable. 136 00:08:04,690 --> 00:08:09,390 That means we can go ahead and print out this variable inside of the console. 137 00:08:09,400 --> 00:08:11,200 And let's go ahead and see what we get. 138 00:08:11,960 --> 00:08:14,330 So now here's our two strings joined together. 139 00:08:14,330 --> 00:08:15,350 It says world. 140 00:08:15,350 --> 00:08:16,520 It's a great day. 141 00:08:16,550 --> 00:08:17,810 Now you may be seeing. 142 00:08:17,810 --> 00:08:20,810 Hey, there's no space between those two words. 143 00:08:20,810 --> 00:08:21,890 How can we fix that? 144 00:08:21,890 --> 00:08:23,990 Well, that's also very easy to do. 145 00:08:24,020 --> 00:08:25,700 We'll go back to our script. 146 00:08:25,970 --> 00:08:32,240 And what we can do is we're going to put a quotation mark here and then another one. 147 00:08:32,240 --> 00:08:34,250 So this is a new string data type right here. 148 00:08:34,250 --> 00:08:36,440 And then we're going to add another two dots. 149 00:08:36,740 --> 00:08:39,980 And then inside of the string we're just going to put a space. 150 00:08:39,980 --> 00:08:43,520 So now we are joining these three different strings together. 151 00:08:43,520 --> 00:08:48,320 And hopefully now we should have a space between these two different strings. 152 00:08:48,320 --> 00:08:49,880 So if we hit run again. 153 00:08:51,080 --> 00:08:53,660 As you can see, we now have world. 154 00:08:53,660 --> 00:08:56,900 It's a great day and there is a space between world. 155 00:08:56,900 --> 00:08:58,040 And it's. 156 00:08:58,490 --> 00:09:02,840 One last thing I would like to mention in this lecture are escape characters. 157 00:09:02,930 --> 00:09:05,960 What exactly is an escape character? 158 00:09:05,990 --> 00:09:11,630 Well, an escape character is a special character format that doesn't represent itself as a literal 159 00:09:11,630 --> 00:09:12,170 string. 160 00:09:12,170 --> 00:09:15,970 Instead, it performs a special action. 161 00:09:15,980 --> 00:09:19,770 You might not know what I mean, but I'm going to demonstrate it to you here in a moment. 162 00:09:19,790 --> 00:09:24,260 What I'm going to do is I'm going to create a new variable again, and I'm going to call this variable 163 00:09:24,290 --> 00:09:25,910 escape characters. 164 00:09:25,910 --> 00:09:28,940 And we're going to set it equal to a new string. 165 00:09:28,940 --> 00:09:36,440 And we can say something like and Bob said today was a crazy day. 166 00:09:36,680 --> 00:09:43,730 Now you might be wondering, how can we include quotations in our text when quotations denote a string 167 00:09:43,730 --> 00:09:44,240 data type? 168 00:09:44,240 --> 00:09:44,600 Right. 169 00:09:44,630 --> 00:09:50,000 We're running into a problem here where we would like to include these quotation marks, but the interpreter 170 00:09:50,000 --> 00:09:51,350 doesn't know what's going on. 171 00:09:51,350 --> 00:09:54,560 Well, that's where an escape character would come into play. 172 00:09:54,860 --> 00:10:01,970 For example, here, this escape character, which is backslash, allows us to include this quotation 173 00:10:01,970 --> 00:10:03,320 mark into the string. 174 00:10:03,320 --> 00:10:10,100 This escape character here has a very special action to omit whatever is in front of it and include 175 00:10:10,100 --> 00:10:11,270 it into the string. 176 00:10:11,270 --> 00:10:14,060 And I can do it the exact same way here as well. 177 00:10:14,060 --> 00:10:20,720 So now this quotation mark here and this quotation mark here are going to be included inside of the 178 00:10:20,720 --> 00:10:21,320 string. 179 00:10:21,320 --> 00:10:24,440 This is the role of this particular escape character. 180 00:10:24,530 --> 00:10:31,850 Another escape character is where we can print all of the text after it on a new line, and that is 181 00:10:31,850 --> 00:10:34,100 the escape character of another backslash. 182 00:10:34,100 --> 00:10:35,840 And then we pass n. 183 00:10:35,840 --> 00:10:41,600 So what this is going to do is it's going to grab all of the text after it, and it's going to print 184 00:10:41,600 --> 00:10:44,690 it on a new line, hence the n. 185 00:10:44,690 --> 00:10:49,940 So we can go ahead and print out escape characters and you'll see exactly what I mean. 186 00:10:49,940 --> 00:10:51,350 So if we hit run here. 187 00:10:53,490 --> 00:10:58,050 We actually got an error attempt to call a nil value on line 28. 188 00:10:58,080 --> 00:11:00,990 I always love errors because we get to solve them in real time. 189 00:11:00,990 --> 00:11:06,180 So if we go to line 28, oh, it's because I accidentally misspelled print. 190 00:11:06,880 --> 00:11:07,960 My bad. 191 00:11:08,230 --> 00:11:10,200 Let's go ahead and try that again. 192 00:11:10,210 --> 00:11:11,140 We'll hit run. 193 00:11:12,950 --> 00:11:13,940 And there we go. 194 00:11:13,970 --> 00:11:14,540 It says. 195 00:11:14,570 --> 00:11:20,630 And Bob said, we get a new line created where this second part of the string is printed, and it says, 196 00:11:20,630 --> 00:11:21,950 today was a crazy day. 197 00:11:21,950 --> 00:11:25,490 And as you can see, it has included our quotation marks. 198 00:11:25,490 --> 00:11:31,520 So that was the role of that first escape character, the single backslash, and then that other escape 199 00:11:31,520 --> 00:11:36,770 character, the backslash n moved all of this other text in our string to a new line. 200 00:11:37,830 --> 00:11:41,880 Another escape character I like to demonstrate to you is backslash t. 201 00:11:42,210 --> 00:11:46,260 So what I'm going to do is I'm going to set escape characters equal to a new string. 202 00:11:46,260 --> 00:11:53,190 And I'm just going to say example of and we can do backslash t escape character. 203 00:11:54,670 --> 00:12:00,730 Now, if I would actually like to include backslash t inside of the text, what I'm going to have to 204 00:12:00,730 --> 00:12:06,970 do here is I'm going to have to create another backslash t, but instead I'm going to put another slash 205 00:12:06,970 --> 00:12:12,490 which is going to omit this original slash, which means these two characters right here are going to 206 00:12:12,490 --> 00:12:14,800 be included inside of my string. 207 00:12:14,800 --> 00:12:18,010 And then afterwards we have our escape character right here. 208 00:12:18,010 --> 00:12:23,290 And what this is going to do is it's going to insert a horizontal space within our string. 209 00:12:23,290 --> 00:12:28,660 So that means example of slash T and then escape character. 210 00:12:28,660 --> 00:12:31,210 There's going to be a big gap between these two. 211 00:12:31,570 --> 00:12:36,460 So let me print this out inside of the console and let me not misspell it again. 212 00:12:36,610 --> 00:12:38,080 And then we can hit run. 213 00:12:39,330 --> 00:12:42,900 And as you can see, we get an example of backslash t. 214 00:12:42,930 --> 00:12:46,080 We get our gap and then escape character. 215 00:12:46,080 --> 00:12:51,450 So as you can see it has inserted a horizontal spacing between these two strings. 216 00:12:52,370 --> 00:12:57,410 Now, these were just some of the main tools at your disposal to manipulate strings. 217 00:12:57,410 --> 00:13:03,020 You should now have a good understanding of the string library and text manipulation inside of your 218 00:13:03,020 --> 00:13:03,670 scripts. 219 00:13:03,680 --> 00:13:05,900 I will see you in the next lecture.